//
//  SwiftUIView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/6/19.
//

import SwiftUI
import URLImage
import UTEBluetoothRYApi


 

struct ProfileView: View {
    @StateObject var profileModel = profileViewModel()

    @State private var showUnderline = true
    @EnvironmentObject var appState: AppState
    @State var items:[UTEModelDevice] = DeviceManager.shared.uteDevices

    var body: some View {
            ScrollView {
                VStack {
                    Image("25")
                        .resizable()
                        .frame(height: 200)
                        .scaledToFit()
                    VStack {
                        HStack {
                            if let avatar = profileModel.userinfo?.data.avatar,
                               let fullURL = URL(string: "http://120.24.168.207:9090\(avatar)") {
                                URLImage(url:fullURL) { image in
                                    image
                                        .resizable()
                                        .aspectRatio(contentMode: .fill)
                                        .frame(width: 90, height: 90)
                                        .clipShape(Circle())
                                        .padding(.leading,42)
                                }
                            } else {
                                Image("21")
                                    .resizable()
                                    .frame(width: 90,height: 90)
                                    .padding(.leading,42)
                                    .clipShape(
                                        RoundedRectangle(cornerRadius: 45, style: .continuous)
                                    )
                                    .scaledToFit()
                            }
                            Image("7056")
                                .resizable()
                                .frame(width: 24,height: 24)
                                .padding(.top,59)
                            NavigationLink("好友"){
                                
                                
                            }
                            .frame(width: 30,height: 24)
                            .padding(.top,59)
                            .foregroundStyle(Color(hex: "#C9C9C9"))
                            .font(.system(size: 14))
                            
                            NavigationLink("99"){
                                
                                
                            }
                            .frame(width: 30,height: 24)
                            .padding(.top,59)
                            .padding(.leading,-4)
                            .foregroundStyle(Color(hex: "#FFFFFF"))
                            .font(.system(size: 14))
                            Divider()
                                .gradientBackground(colors: [.white], cornerRadius: 0)
                                .frame(height: 10)
                                .padding(.top,59)
                                .padding(.leading,8)
                            Image("7055")
                                .resizable()
                                .frame(width: 24,height: 24)
                                .padding(.top,59)
                                .padding(.leading,10)
                            Button(action: {
                                
                            }, label: {
                                Text("动能")
                            })
                            .frame(width: 30,height: 24)
                            .padding(.top,59)
                            .foregroundStyle(Color(hex: "#C9C9C9"))
                            .font(.system(size: 14))
                            NavigationLink("99"){
                                
                                
                            }
                            .frame(width: 30,height: 24)
                            .padding(.top,59)
                            .padding(.leading,-4)
                            .foregroundStyle(Color(hex: "#FFFFFF"))
                            .font(.system(size: 14))
                            Spacer()
                        }
                        .frame(width: UIScreen.main.bounds.width,height: 90)
                        .background(.clear)
                        .padding(.top,-45)
                        HStack {
                            Text(profileModel.userinfo?.data.userName ?? "")//"Motivation"
                                .font(.system(size: 20))
                                .foregroundStyle(Color(hex: "#FFFFFF"))
                                .frame(width: 100)
                                .padding(.leading,36)
                            Spacer()
                                HStack {
                                    Button(action: {
                                        appState.navigateTo(.usrinfo)
                                    }, label: {
                                        Text("个人信息")
                                    })
                                        .frame(width: 100,height: 30)
                                        .font(.system(size: 14))
                                        .foregroundStyle(Color(hex: "#B2B2B2"))
                                        .overlay(
                                            RoundedRectangle(cornerRadius: 15)
                                                .stroke(Color(hex: "#B2B2B2"), lineWidth: 0.5)
                                        )
                                }.padding(.trailing,12)
                            
                                HStack {
                                    Button(action: {
                                        // 二维码分享
                                        
                                        
                                        
                                    }) {
                                        Image("7243")
                                            .frame(width: 30,height: 30)
                                    }
                                } .padding(.trailing,12)

                                HStack {
                                    Button(action: {
                                        //设置
                                        appState.navigateTo(.setting)
                                    }) {
                                        Image("7244")
                                            .frame(width: 30,height: 30)
                                    }
                                }.padding(.trailing,18)
                        }.frame(width: UIScreen.main.bounds.width,height: 50)
                            .background(.clear)
                            .padding(.top,0)
                        HStack {
                            VStack {
                                HStack {
                                    Text("运动数据")
                                        .frame(width: 66)
                                        .font(.system(size: 16))
                                        .foregroundStyle(Color(hex: "#FFFFFF"))
                                    NavigationLink(destination: HomeView()) {
                                        HStack {
                                            Image("xiangyou")
                                                .frame(width: 16,height: 16)
                                        } .padding(.leading,0)
                                    }
                                    Spacer()
                                }
                                .padding(.leading,20)
                                .padding(.top,20)
                                HStack {
                                    Text("90")
                                        .foregroundStyle(Color(hex: "#FFFFFF"))
                                        .font(.system(size: 24))
                                        .padding(.leading,20)
                                        .padding(.top,2)
                                    
                                    Text("分钟")
                                        .foregroundStyle(Color(hex: "#FFFFFF"))
                                        .font(.system(size: 12))
                                        .padding(.leading,-2)
                                        .padding(.top,2)
                                    Spacer()
                                }
                                HStack {
                                    Image("xiangxia")
                                        .resizable()
                                        .frame(width: 10,height: 5)
                                        .padding(.leading,20)
                                    Text("11.2%")
                                        .foregroundStyle(Color(hex: "#EF5F5F"))
                                        .font(.system(size: 14))
                                        .padding(.leading,-2)
                                        .padding(.top,2)
                                    
                                    Text("较上周")
                                        .foregroundStyle(Color(hex: "#B2B2B2"))
                                        .font(.system(size: 14))
                                        .padding(.leading,-2)
                                        .padding(.top,2)
                                    Spacer()
                                }
                                Spacer()
                            }
                            .frame(width: 180, height: 118)
                            .background(.clear)
                            VStack {
                                Image("6521750332421_.pic")
                                    .resizable()
                                    .frame(width: 133,height: 44)
                            }
                            Spacer()
                        }
                        .frame(height: 118)
                        .background(Color(hex: "#1E1E1E"))
                        .cornerRadius(16.0)
                        .padding(.horizontal, 12)
                        HStack {
                            Spacer()
                            Button {
                                showUnderline = true
                            } label: {
                                VStack {
                                    Text("我的设备")
                                        .font(.system(size: 16))
                                        .foregroundStyle(showUnderline ? Color(hex: "#FFFFFF") : Color(hex: "#B2B2B2"))
                                        .padding(.bottom,2)
                                    Image(showUnderline ? "6456" : "none")
                                        .frame(width: 24,height: 3)
                                        .padding(.bottom,0)
                                }
                            }
                            .frame(width: 80,height: 44)
                            Spacer()
                            Button {
                                showUnderline = false
                            } label: {
                                VStack {
                                    Text("我的动态")
                                        .font(.system(size: 16))
                                        .foregroundStyle(showUnderline ? Color(hex: "#B2B2B2") : Color(hex: "#FFFFFF"))
                                        .padding(.bottom,2)
                                    Image(showUnderline ? "none" : "6456")
                                        .frame(width: 24,height: 3)
                                        .padding(.bottom,0)
                                }
                            }
                            .frame(width: 80,height: 44)
                            Spacer()
                        }
                        .background(.clear)
                        .frame(height: 44)
                    }
                    .frame(width: Bounds.width, height: 286)
                    .gradientBackground(colors: [Color(hex: "#0A0A0A")], cornerRadius: 40)
                    .padding(.top,-50)
                    .padding(.leading,0)
                    .padding(.trailing,0)
                    HStack {
                        Image(showUnderline ? "6623" : "7054")
                            .resizable()
                            .scaledToFill()
                            .padding(.leading,20)
                            .frame(width: 32,height: 32)
                        
                        Text(showUnderline ? "设备管理" : "运动圈")
                            .font(.system(size: 16))
                            .foregroundStyle(Color(hex: "#FFFFFF"))
                            .padding(.leading,8)
                        Spacer()
                        Button {
                            if showUnderline {
                                appState.navigateTo(.deviceList)
                            }else {
                                appState.navigateTo(.dynamicMoment)
                            }
                        } label: {
                            Image("xiangyou")
                                .frame(width: 24,height: 20)
                                .padding(.trailing,20)
                        }
                    }
                    .frame(height: 56)
                    .gradientBackground(colors: [Color(hex: "#1E1E1E")], cornerRadius: 16)
                    .padding(.horizontal, 12)
                    if showUnderline {
                        ForEach($items, id: \.identifier) { $device in
                            DevicesView(model: $device)
                                .onTapGesture {
                                    print("点击了单元格")
                                    appState.navigateTo(.deviceSetting(id: device))
                                }
                        }
                    }else {
                        DynamicListView(items: $profileModel.dynamicList)
                    }
                }
            }
            .scrollIndicators(.hidden)
            .navigationBarHidden(true)
            .background(Color(hex: "#0A0A0A"))
            .edgesIgnoringSafeArea(.all)
            .onAppear {
                profileModel.getDynamicListData()
            }
        }
}


struct DynamicListView: View {
    @Binding var items: [DynamicItem]
    @EnvironmentObject var appState: AppState
    var body: some View {
        ForEach($items, id: \.dynamicId) { $item in
            MySportCircleView(dynamicitem: $item).onTapGesture {
                print("点击了运动圈单元格")
                appState.navigateTo(.sportCircleDetail(id: item))
            }
        }
    }
}


#Preview {
    ProfileView()
}
